home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / dbwrendr.zip / SOURCE / DISPLAY.C < prev    next >
Text File  |  1989-11-04  |  19KB  |  664 lines

  1. /************************************************************/
  2. /*                                                          */
  3. /*   changes -                                              */
  4. /*   10/31/89  ver 1.02 - allow for new maxcol and maxrow   */
  5. /*             integers at front of temp file.  They are    */
  6. /*             scanned off & ignored, for now               */
  7. /*                                                          */ 
  8. /************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <fcntl.h>
  13. #include <io.h>
  14. #include <sys\types.h>
  15. #include <sys\stat.h>
  16. #include <graph.h>
  17.  
  18. #include "display.h"
  19.  
  20. #define VERSION "\nDISPLAY v1.02 (IBM MCGA/VGA)\nCopyright (C) 1989 J. Lowery.  All rights reserved.\n\n"
  21.  
  22. struct {
  23.      int       curr;
  24.      int       fp;
  25.        } ifil[10];
  26.  
  27. static int     numfiles = 0;
  28. static int     colorstats[TMPCOLORS][2];
  29. static int     image[MAXCOL];
  30. static char    fname[40],outname[40],str[40];
  31. static int     usedColors;
  32. static int     giffile;
  33.  
  34. /* 'ofil' must be accessable to gif output functions in another module */
  35.        int     ofil;
  36.  
  37. /* buffer for a single scan line */
  38. static scanlinetype line;
  39.  
  40.  
  41.  
  42. /* GIF header mode byte definitions */
  43.  
  44. #define GLOBALCOLORMAP 0x80
  45. #define COLORRES       ((DEPTH-1) << 4)
  46. #define BITSPERPIXEL   (BITS-1)
  47.  
  48. /* 
  49.  *   the following structures are odd lengths and mix byte- and 
  50.  *   word-length objects, so MUST be packed on 1-byte boundaries, 
  51.  *   or file operations break.
  52.  */
  53.  
  54. #pragma pack(1)
  55.  
  56. struct {
  57.      char signature[6];
  58.      int  screenWidth;
  59.      int  screenHeight;
  60.  
  61.      byte mode;
  62.      byte background;
  63.      byte empty;
  64.  
  65.      } gifHeader = { "GIF87a", MAXCOL, MAXROW, 
  66.                      GLOBALCOLORMAP | COLORRES | BITSPERPIXEL, 0, 0 };
  67.  
  68.  
  69. struct {
  70.      char separator;
  71.      int  imageLeft;
  72.      int  imageTop;
  73.      int  imageWidth;
  74.      int  imageHeight;
  75.      byte mode;
  76.      byte codeSize;      /* this is actually part of the image data stream */
  77.  
  78.      } imageHeader = { ',', 0, 0, MAXCOL, MAXROW, BITSPERPIXEL, BITS }; 
  79.  
  80. /* back to normal structure packing */
  81. #pragma pack()
  82.  
  83. /*   
  84.  *   display write error, wait for operator acknowledge, 
  85.  *   reset video & return.  This is used while video is in 320x200
  86.  *   mode. 
  87.  */
  88.  
  89. int writeError()
  90. {
  91.      close( ofil );           /* close the file   */
  92.  
  93.      _settextposition( 16, 0 );
  94.      printf("Write error in file %s\n", outname);
  95.      printf("Press any key to exit...");
  96.      while (!kbhit())
  97.           ;
  98.      getch();
  99.      return( TRUE );
  100. }
  101.  
  102. int gifHdr()      /* create a GIF file from screen */
  103. {
  104.      byte colorValue[3];      /* a single rgb triplet */
  105.      int  i;
  106.  
  107.      /* write the header information */
  108.      if (write(ofil, (char *)&gifHeader, sizeof(gifHeader)) != sizeof(gifHeader))
  109.           return( writeError() );
  110.  
  111.      /* write the global color map   */
  112.      for (i=0; i<COLORS; i++)
  113.      {
  114.           /* 
  115.            *   for each of our 256 possible colors, 
  116.            *   map the color's 3 primary intesities (0..15) into 
  117.            *   the gif color map color intensities (0..255).
  118.            *   Note that i==0 defines the MCGA border color.
  119.            */
  120.  
  121.           if (i < usedColors)
  122.           {
  123.                colorValue[2] = ((long)(colorstats[i][0] & 0x0F00) >> 4); /* blue  */
  124.                colorValue[1] = ((long)(colorstats[i][0] & 0x00F0) );     /* green */
  125.                colorValue[0] = ((long)(colorstats[i][0] & 0x000F) << 4 ); /* red   */
  126.           }
  127.           else
  128.           {
  129.                colorValue[0] = 0;
  130.                colorValue[1] = 0;
  131.                colorValue[2] = 0;
  132.           }
  133.  
  134.           if (write( ofil, colorValue, sizeof(colorValue)) != sizeof(colorValue))
  135.                return( writeError() );
  136.      }
  137.      /* write the image descriptor */
  138.      if (write(ofil, (char *)&imageHeader, sizeof(imageHeader)) != sizeof(imageHeader))
  139.           return( writeError() );
  140.  
  141.      return(0);       /* header complete, return ok */ 
  142. }
  143.  
  144.  
  145. char terminal[2] = { 0, ';' };     /* image terminator sequence */
  146.  
  147. int gifClose()
  148. {
  149.  
  150.      if (gifFlush())
  151.           return(TRUE);
  152.      if (write( ofil, terminal, sizeof( terminal )) != sizeof( terminal ))
  153.           return( writeError() );
  154.  
  155.      close( ofil );
  156.      return(FALSE);      /* no error */     
  157. }
  158.  
  159. /* reset video mode, print error message and exit */
  160.  
  161. void error( msg )
  162. char *msg;
  163. {
  164.      _setvideomode( _DEFAULTMODE );
  165.      printf( "ERROR: %s\n", msg );
  166.      exit( -1 );
  167. }
  168.  
  169. /* print error message and exit.  Used in 80-column text mode */
  170.  
  171. void usageExit( msg )
  172. char *msg;
  173. {
  174.      if (msg) 
  175.           printf("ERROR: %s\n",msg);
  176.  
  177.      printf("\nUsage: raydsp [-g outfile] infile [infile [...]] \n");
  178.      printf("\nWhere: -g       Creates GIF file from screen display");
  179.      printf("\n       outfile  GIF output file name.");
  180.      printf("\n       infile   Input DBW_Render .TMP file.\n");
  181.      printf("\nDisplays a DBW_Render output file, and optionally ");
  182.      printf("\nconverts it to Compuserve (tm) GIF format.\n\n");
  183.      exit(-1);
  184. }
  185.  
  186. read_scanline(pass,row)
  187. int pass,row;
  188. {
  189.      int i,j,x,val,good;
  190.  
  191.      if (pass < 2)
  192.      {    if ((row % 50) == 0) 
  193.                printf("\nRow: %4d ",row);
  194.           printf(".");
  195.           fflush(stdout);
  196.      }
  197.  
  198.      good = 0;
  199.      for (i = 0; i < numfiles && good == 0; i++) 
  200.      {
  201.           while (ifil[i].curr < row) 
  202.           {
  203.                if (read(ifil[i].fp,(char *)&ifil[i].curr,sizeof(int))!=sizeof(int)) 
  204.                {
  205.                     ifil[i].curr = 9999; 
  206.                     continue;
  207.                }
  208.  
  209.                if (read(ifil[i].fp,(char *)line,sizeof(scanlinetype))!=sizeof(scanlinetype))
  210.                {
  211.                     ifil[i].curr = 9999;
  212.                     continue;
  213.                }
  214.                if (ifil[i].curr == row) 
  215.                {
  216.                     good = 1;
  217.                     break;
  218.                }
  219.           }
  220.      }
  221.  
  222.     /* couldn't find this scan line in any file */
  223.  
  224.      if (good == 0) 
  225.           return(0);
  226.  
  227.      x = 0;
  228.      for (i = 0; i < WPSL; i++) 
  229.      {
  230.           for (j = 0; j < PPW; j++) 
  231.           {
  232.           /*--- TMP file has red, green, then blue nibbles.  Palette
  233.                 needs them as blue (MSB), green, red (LSB) ----------*/
  234.  
  235.                val = (  ((line[RED  ][i] >> (BPP * j)) & (MAXGRAY-1))               /* red   */
  236.                      + (((line[GREEN][i] >> (BPP * j)) & (MAXGRAY-1)) <<  BPP)      /* green */
  237.                      + (((line[BLUE ][i] >> (BPP * j)) & (MAXGRAY-1)) << (BPP*2))); /* blue  */
  238.  
  239.                val %= TMPCOLORS;
  240.  
  241.                if (pass == 1)      /* histogram count on pass 1 */
  242.                {
  243.                     if (colorstats[val][1] < 32767) 
  244.                          colorstats[val][1]++;
  245.                }
  246.                else 
  247.                     image[x++] = val;
  248.           }
  249.      }
  250.      return(1);
  251. }
  252.  
  253. /* 
  254.  *   find the darkest color in colorstats[], and make it
  255.  *   colorstats[0], for use as a border color
  256.  */
  257.  
  258. getBackground()
  259. {
  260.      /* scan the color table for minimal distance to r,g,b = {0,0,0} */
  261.  
  262.      int i, j;
  263.      short best, dist;
  264.      short t0, t1;
  265.  
  266.      for ( i=0, best=0, dist=32000; i < usedColors; i++) 
  267.      {
  268.           j = distance( colorstats[i][0], 0 );
  269.           if ( j < dist) 
  270.           {
  271.                best = i;
  272.                dist = j;
  273.           }
  274.      }
  275.  
  276.      /* colorstats[0] gets darkest color for background */
  277.  
  278.      t0 = colorstats[ best ][ 0 ];
  279.      t1 = colorstats[ best ][ 1 ];
  280.      colorstats[ best ][ 0 ] = colorstats[ 0 ][ 0 ];  
  281.      colorstats[ best ][ 1 ] = colorstats[ 0 ][ 1 ];
  282.      colorstats[ 0 ][ 0 ] = t0;
  283.      colorstats[ 0 ][ 1 ] = t1;
  284.  
  285.  
  286. }
  287.  
  288. main(argc,argv)
  289. int        argc;
  290. char        **argv;
  291. {
  292.      short  i, j, k;
  293.      short  xRes, yRes;
  294.      short  gap;
  295.      short  t0, t1, prgb, crgb, cpix, ppix, maxdis;
  296.      char   *s;
  297.      char   errmsg[80];
  298.  
  299.  
  300.      printf(VERSION);
  301.